#include "module_weather_impl.h"


namespace ModuleManagement
{
  void CallbackNewRequest(void* param)
  {
    WeatherModuleImpl* module = reinterpret_cast<WeatherModuleImpl*>(param);
    if (module)
    {

    }
  }
}


namespace ModuleManagement
{
  WeatherModuleImpl::WeatherModuleImpl()
    : OutputStream(NULL)
    , Thread(NULL)
    , NewRequestEvent(NULL)
    throw(Common::Exception)
  {
    CreateSyncObjects();
  }

  WeatherModuleImpl::~WeatherModuleImpl()
  {
    // thread can be run in this moment
    WaitForSingleObject(Thread, INFINITE);
    DestroySyncObjects();
  }

  void WeatherModuleImpl::GetWeatherResponse(std::string &response)
  {
    // create process thread
    DWORD dwThreadId;
    Thread = CreateThread(NULL, 0, &ThreadFunc, (LPVOID)this, 0, dwThreadId);
    if (Thread == NULL)
      throw Common::Exception("EXCEPTION - Unable to create module thread");

    // wait stop of thread
    WaitForSingleObject(Thread, INFINITE);
    response = WeatherResponse;
  }

  void WeatherModuleImpl::Start()
  {
  }

  void WeatherModuleImpl::Stop()
  {
  }

  void WeatherModuleImpl::CreateSyncObjects() throw(Common::Exception)
  {
    NewRequestEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (NewRequestEvent == NULL)
      throw Common::Exception("EXCEPTION - Unable to create event object");
  }

  void WeatherModuleImpl::DestroySyncObjects()
  {
    CloseHandle(NewRequestEvent);
  }

  DWORD WINAPI WeatherModuleImpl::ThreadFunc(LPVOID param)
  {
    WeatherModule* module = reinterpret_cast<WeatherModule*>(param);
    if (module == NULL)
      ExitThread(EXIT_FAILURE);

    module->WeatherResponse.empty();
    ExitThread(EXIT_SUCCESS);
  }
}